package org.salatec.tapestry4e.util;

import org.eclipse.core.resources.IFile;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

public class EditorUtils {

	/**
	 * @param theFile
	 */
	public static void openFileInEditor(IFile theFile) {

		IWorkbenchPage activePage = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage();
		try {
			IDE.openEditor(activePage, theFile, true);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	public static IFile findActiveFile() {
		IWorkbenchPage activePage = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage();
		IEditorPart editor = activePage.getActiveEditor();
		if (editor != null) {
			IEditorInput input = editor.getEditorInput();
			if (input != null) {
				IFile currentFile = (input instanceof IFileEditorInput) ? ((IFileEditorInput) input)
						.getFile()
						: null;
				return currentFile;
			}
		}
		return null;

	}

}
